<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_mathExpression"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
            border: 1px solid #3473b7;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_mathExpression"></h5></div>
    <div class='panel-body content'>
        <div class='panel'>
            <div class='input-group'>
                <span data-i18n="resources.text_expression"></span><span>[Jingjin.JingjinTerrain] + 600</span>
            </div>
        </div>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_mathExpression"
               onclick="addResultLayer()"/>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_clearLayer"
               onclick="removeTheme()"/>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script>
    var map, baseLayer, resultLayer,
        host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090",
        url = host + "/iserver/services/map-jingjin/rest/maps/京津地区地图",
        url2 = host + "/iserver/services/spatialanalyst-sample/restjsr/spatialanalyst";

    init();

    function init() {
        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                })]
        });
        map.addControl(new SuperMap.Control.LayerSwitcher(), new SuperMap.Pixel(42, 80));

        baseLayer = new SuperMap.Layer.TiledDynamicRESTLayer("京津地区地图", url, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        baseLayer.events.on({"layerInitialized": addLayer});
    }

    function addLayer() {
        map.addLayer(baseLayer);
        map.setCenter(new SuperMap.LonLat(117.2, 40.11), 0);
        map.allOverlays = true;
    }

    function addResultLayer() {
        widgets.alert.clearAlert();
        removeTheme();
        //新建一个栅格代数运算服务
        var mathExpressionAnalysisService = new SuperMap.REST.MathExpressionAnalysisService(url2, {
            eventListeners: {
                "processCompleted": mathExpressionAnalysisCompleted,
                "processFailed": mathExpressionAnalysisFailed
            }
        });
        //新建一个栅格代数运算参数对象
        var mathExpressionAnalysisParameters = new SuperMap.REST.MathExpressionAnalysisParameters({
            //指定数据集
            dataset: "JingjinTerrain@Jingjin",
            /*
             //指定范围
             extractRegion: new SuperMap.Geometry.Polygon([
                 new SuperMap.Geometry.LinearRing([
                new SuperMap.Geometry.Point(116, 39),
                new SuperMap.Geometry.Point(117, 39),
                new SuperMap.Geometry.Point(117, 40),
                new SuperMap.Geometry.Point(116, 40)
                ])
             ]),
             */
            //要执行的栅格运算代数表达式
            "expression": "[Jingjin.JingjinTerrain] + 600",
            //存储结果数据集的数据源
            "targetDatasource": "Jingjin",
            //结果数据集名称
            "resultGridName": "MathExpressionAnalysis_Result",
            deleteExistResultDataset: true
        });

        mathExpressionAnalysisService.processAsync(mathExpressionAnalysisParameters);
    }

    //用栅格专题图展示分析结果
    function mathExpressionAnalysisCompleted(mathExpressionAnalysis) {
        var color1 = new SuperMap.REST.ServerColor(40, 140, 40),
            color2 = new SuperMap.REST.ServerColor(170, 198, 40),
            color3 = new SuperMap.REST.ServerColor(191, 191, 0),
            color4 = new SuperMap.REST.ServerColor(191, 95, 0),
            color5 = new SuperMap.REST.ServerColor(127, 0, 0),

            themeGridRangeIteme1 = new SuperMap.REST.ThemeGridRangeItem({
                start: 0,
                end: 650,
                color: color1
            }),
            themeGridRangeIteme2 = new SuperMap.REST.ThemeGridRangeItem({
                start: 650,
                end: 750,
                color: color2
            }),
            themeGridRangeIteme3 = new SuperMap.REST.ThemeGridRangeItem({
                start: 750,
                end: 900,
                color: color3
            }),
            themeGridRangeIteme4 = new SuperMap.REST.ThemeGridRangeItem({
                start: 900,
                end: 1350,
                color: color4
            }),
            themeGridRangeIteme5 = new SuperMap.REST.ThemeGridRangeItem({
                start: 1350,
                end: 3600,
                color: color5
            }),

            themeGridRange = new SuperMap.REST.ThemeGridRange({
                reverseColor: false,
                rangeMode: SuperMap.REST.RangeMode.EQUALINTERVAL,
                //栅格分段专题图子项数组
                items: [themeGridRangeIteme1,
                    themeGridRangeIteme2,
                    themeGridRangeIteme3,
                    themeGridRangeIteme4,
                    themeGridRangeIteme5
                ]
            }),
            themeParameters = new SuperMap.REST.ThemeParameters({
                //制作专题图的数据集（栅格代数运算的结果数据集）
                datasetNames: [mathExpressionAnalysis.result.dataset.split('@')[0]],
                dataSourceNames: ["Jingjin"],
                joinItems: null,
                //专题图对象列表
                themes: [themeGridRange],
                types: ['REGION']
            });
        var themeService = new SuperMap.REST.ThemeService(url, {
            eventListeners: {
                "processCompleted": themeCompleted,
                "processFailed": themeFailed
            }
        });
        themeService.processAsync(themeParameters);
    }

    function themeCompleted(themeEventArgs) {
        if (themeEventArgs.result.resourceInfo.id) {
            resultLayer = new SuperMap.Layer.TiledDynamicRESTLayer("栅格代数运算结果", url, {
                cacheEnabled: false,
                transparent: true,
                layersID: themeEventArgs.result.resourceInfo.id
            }, {"maxResolution": "auto"});
            resultLayer.events.on({"layerInitialized": addThemelayer});
        }
    }

    function addThemelayer() {
        resultLayer.setOpacity(0.8);
        map.addLayer(resultLayer);
    }

    function themeFailed(serviceFailedEventArgs) {
        widgets.alert.showAlert(serviceFailedEventArgs.error.errorMsg, false);
    }

    function mathExpressionAnalysisFailed(serviceFailedEventArgs) {
        widgets.alert.showAlert(serviceFailedEventArgs.error.errorMsg, false);
    }

    //移除专题图
    function removeTheme() {
        widgets.alert.clearAlert();
        if (map.layers.length > 1) {
            map.removeLayer(resultLayer, true);
        }
    }

</script>

</body>
</html>